package com.zhudeshuai.zhudemolist.widget.BarChart;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;

import com.zhu.zhuCore.utils.EmptyUtil;
import com.zhu.zhuCore.utils.LogUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 柱形图对象
 *
 * @author zhudeshuai
 * @since 16/7/1
 */
public class BarChar {

    private static final java.lang.String TAG = "BarChar";
    private static final float AXIS_WIDTH = 3;
    private static final float INNER_AXIS_WIDTH = 1;
    private static final float TEXT_SIZE = 20;
    private final float mStart;
    private final float mEnd;
    private final int mVerticalCount;
    //1.背景数据
    /**
     * 背景范围
     */
    private RectF mRectFBg = new RectF();

    /**
     * 边距
     */
    private int mShowTextWidth = 40;

    /**
     * 左边显示文本宽度
     */
    private int mShowLeftWidth = 80;
    /**
     * 背景坐标轴画笔
     */
    private Paint mAxisPaint = new Paint();
    /**
     * 网格线画笔
     */
    private Paint mAxisLinePaint = new Paint();
    /**
     * 坐标轴点集合
     */
    private float[] mAxisPts;

    /**
     * 内部竖线点集合
     */
    private float[] mVerticalLinePts;

    /**
     * 内部横线点集合
     */
    private float[] mHorizontalLinePts;

    /**
     * 坐标轴文案信息集合
     */
    private List<AxisInfo> mAxisInfoList = new ArrayList<>();
    /**
     * 坐标轴文案画笔
     */
    private Paint mAxisInfoPaint = new Paint();

    /**
     * 显示的数据
     */
    private List<BarChartData> mShowData = new ArrayList<>();

    /**
     * 数据画笔
     */
    private Paint mRectPaint = new Paint();

    private float mAxisLeft;
    private float mAxisRight;
    private float mAxisTop;
    private float mAxisBottom;

    public BarChar(int minValue, int maxValue, int count) {
        this.mStart = minValue;
        this.mEnd = maxValue;
        this.mVerticalCount = count;

        mAxisPaint.setColor(Color.BLUE);
        mAxisPaint.setStrokeWidth(AXIS_WIDTH);

        mAxisLinePaint.setColor(Color.GRAY);
        mAxisLinePaint.setStrokeWidth(INNER_AXIS_WIDTH);

        mAxisInfoPaint.setColor(Color.BLACK);
        mAxisInfoPaint.setTextSize(TEXT_SIZE);

        mRectPaint.setColor(Color.RED);
        mRectPaint.setStyle(Paint.Style.FILL);//设置填满
    }

    /**
     * 设置展示数据
     *
     * @param showData
     */
    public void setShowData(List<BarChartData> showData) {
        this.mShowData.clear();
        this.mShowData.addAll(showData);
    }

    /**
     * 设置背景范围
     *
     * @param left
     * @param top
     * @param right
     * @param bottom
     */
    public void setRectFBg(float left, float top, float right, float bottom) {
        mAxisLeft = left + mShowLeftWidth;
        mAxisRight = right - mShowTextWidth;
        mAxisTop = top + mShowTextWidth;
        mAxisBottom = bottom - mShowTextWidth;
        mRectFBg.set(left, top, right, bottom);
        mAxisPts = new float[]{
                mAxisLeft, mAxisTop, mAxisLeft, mAxisBottom,
                mAxisLeft, mAxisBottom, mAxisRight, mAxisBottom
        };

        calculateVerticalPts();
        calculateVerticalAxis();
        calculateHorizontalData();
    }

    /**
     * 准备横线数据,以及数据文字位置
     */
    private void calculateHorizontalData() {
        float height = mAxisBottom - mAxisTop;
        //显示数据为空
        if (EmptyUtil.isEmpty(mShowData))
            return;
        float perHeight = height / mShowData.size();
        float rectHeight = perHeight * 2 / 3;
        mHorizontalLinePts = new float[mShowData.size() * 4];
        for (int i = 0; i < mShowData.size(); i++) {
            //构造每条横线
            mHorizontalLinePts[i * 4] = (mAxisLeft);
            mHorizontalLinePts[i * 4 + 1] = (mAxisTop + i * perHeight);
            mHorizontalLinePts[i * 4 + 2] = (mAxisRight);
            mHorizontalLinePts[i * 4 + 3] = (mAxisTop + i * perHeight);

            //设置文字位置
            BarChartData item = mShowData.get(i);
            item.mNameX = 0;
            item.mNameY = mAxisTop + (i * perHeight) + perHeight / 2;

            //设置矩形参数
            RectF rectF = item.dataRectF;
            rectF.set(mAxisLeft + AXIS_WIDTH,
                    item.mNameY - rectHeight / 2,
                    mAxisLeft + item.dataLength * (mAxisRight - mAxisLeft) / mEnd,
                    item.mNameY - rectHeight / 2 + rectHeight
            );


        }


    }

    /**
     * 准备竖线数据
     */
    private void calculateVerticalPts() {
        float width = mAxisRight - mAxisLeft;
        float perWidth = width / mVerticalCount;
        mVerticalLinePts = new float[mVerticalCount * 4];
        for (int i = 0; i < mVerticalCount; i++) {
            //构造每条竖线
            mVerticalLinePts[i * 4] = (mAxisLeft + (i + 1) * perWidth);
            mVerticalLinePts[i * 4 + 1] = (mAxisTop);
            mVerticalLinePts[i * 4 + 2] = (mAxisLeft + (i + 1) * perWidth);
            mVerticalLinePts[i * 4 + 3] = (mAxisBottom);
        }
    }

    /**
     * 计算竖线坐标文字
     */
    private void calculateVerticalAxis() {
        float len = mEnd - mStart;
        float perLen = len / mVerticalCount;
        for (int i = 0; i <= mVerticalCount; i++) {
            AxisInfo info = new AxisInfo();
            info.mText = String.valueOf((int) (mStart + i * perLen));
            float textLen = mAxisInfoPaint.measureText(info.mText);
            if (i == 0) {
                info.mX = mAxisLeft - textLen / 2;
                info.mY = mAxisBottom + 20;
            } else {
                info.mX = mVerticalLinePts[(i - 1) * 4 + 2] - textLen / 2;
                info.mY = mVerticalLinePts[(i - 1) * 4 + 3] + 20;
            }
            mAxisInfoList.add(info);
        }

    }

    /**
     * 绘制方法
     *
     * @param canvas
     */
    public void render(Canvas canvas) {
        //1.绘制背景
        //1.1绘制坐标轴
        canvas.drawLines(mAxisPts, mAxisPaint);
        //1.2绘制坐标轴内部竖线
        canvas.drawLines(mVerticalLinePts, mAxisLinePaint);
        //1.3绘制底部坐标文字
        for (AxisInfo info : mAxisInfoList) {
            canvas.drawText(info.mText, info.mX, info.mY, mAxisInfoPaint);
        }
        //2.绘制数据
        //2.1绘制坐标轴内部横线
        canvas.drawLines(mHorizontalLinePts, mAxisLinePaint);
        //2.2绘制坐标轴左边文字,数据矩形
        for (BarChartData data : mShowData) {
            canvas.drawText(data.mName, data.mNameX, data.mNameY, mAxisInfoPaint);
            canvas.drawRect(data.dataRectF, mRectPaint);
        }
    }

}
